package com.ws.shop.action.front;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Article;
import com.ws.shop.entity.ArticleCategories;
import com.ws.shop.service.ArticleCategoriesService;
import com.ws.shop.service.ArticleService;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value = "/news/*")
public class NewsAction   extends BaseFrontAction{

	@Autowired
	private ArticleService articleService;
	@Autowired
	private ArticleCategoriesService articleCategoriesService;
	

	@RequestMapping(value="newsindex.htm",method=RequestMethod.GET)//请求地址
	public String newsindex(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "articleid", 0);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = new Page(pageno,10);
		int state = 1;
		int cid = 1;//公告分类1
		List<Map<String,Object>> gglist = articleService.getArticle(state, cid, 6);
		cid = 2;// 感言分类2
		List<Map<String,Object>> gylist = articleService.getArticle(state, cid, 6);
		Article article = articleService.findArticleById(id);
		cid = article.getCid();
		ArticleCategories ac = articleCategoriesService.findArticleCategoriesById(cid);
		int type = 1;
		List<Map<String,Object>> previousmap = articleService.getArticlesByCondition(id, cid, type);
		type = 2;
		List<Map<String,Object>> nextmap = articleService.getArticlesByCondition(id, cid, type);
		type = 3;
		List<Map<String,Object>> recentmap = articleService.getArticlesByCondition(id, cid, type);
		
		model.addAttribute("gglist", gglist);
		model.addAttribute("gylist", gylist);
		model.addAttribute("ac", ac);
		
		model.addAttribute("previousmap", previousmap);
		model.addAttribute("nextmap", nextmap);
		model.addAttribute("recentmap", recentmap);
		
		model.addAttribute("page", page);
		return "newsindex";
	}
	
	@RequestMapping(value="newslist.htm",method=RequestMethod.GET)//请求地址
	public String newslist(HttpServletRequest request,Model model){
		int cid = RequestUtil.getIntParameter(request, "cid", 0);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		Page page = new Page(pageno,10);
		ArticleCategories ac = articleCategoriesService.findArticleCategoriesById(cid);
		int state = 1;
		articleService.getArticlesByCid(page, cid, state);
		cid = 1;//公告分类1
		List<Map<String,Object>> gglist = articleService.getArticle(state, cid, 6);
		cid = 2;// 感言分类2
		List<Map<String,Object>> gylist = articleService.getArticle(state, cid, 6);

		model.addAttribute("page", page);
		model.addAttribute("gglist", gglist);
		model.addAttribute("gylist", gylist);
		model.addAttribute("ac", ac);
		return "newslist";
	}

}
